package com.example.Arithmetic.Lettcode;



import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 日期：2023/10/14
 * 时间：17:28
 * 描述：逆波兰表达式，又名后缀表达式
 */
public class E04Lettcode150 {
    public int evalRPN(String[] tokens) {
        Deque<Integer> list = new ArrayDeque<>();
        for (String s2 : tokens) {
            switch (s2) {
                case "+" -> {
                    Integer b = list.pollLast();
                    Integer a = list.pollLast();
                    list.offerLast(a + b);
                }
                case "-" -> {
                    Integer b = list.pollLast();
                    Integer a = list.pollLast();
                    list.offerLast(a - b);
                }
                case "*" -> {
                    Integer b = list.pollLast();
                    Integer a = list.pollLast();
                    list.offerLast(a * b);
                }
                case "/" -> {
                    Integer b = list.pollLast();
                    Integer a = list.pollLast();
                    list.offerLast(a / b);
                }
                default -> list.push(Integer.parseInt(s2));
            }
        }
        return list.pollLast();
    }
}
